const app = getApp();

app.func.skulist(
    {
        /**
         * 页面的初始数据
         */
        data: {
            productlist: [],
            categorylist: '',
            pageEnd: false,
            currentTab: 0,
            type: null,   //1推荐   2价格   3折扣  4筛选
            min: null,
            max: null,
            showbox: false,  //隐藏筛选框
            chooseindex: 4,  //筛选框默认选择全部搜索
            sort: '',
            id: 0,
            myinfo:'',
            currindex:1,
            tabname:'商品分类',
            scrollHeight:'',
            skunameList:[],
            scrolltop:"0"
        },

        collectChange(e){
           let data = e.detail;
           this.setData({
               [`productlist[${this.data.currentTab}][${data.index}].add_cart`]:data.value
           });
        },

        categories: function () {
            app.func.getPromise('/categories')
                .then(([code, res]) => {
                  console.log(res)
                    if (code == 200) {
                      let lenegs= res.data.length;
                      let heidgt=lenegs*100
                      this.setData({
                        runlist:res.data,
                      })
                        // res.data.splice(1, 2);
                        this.initData(res);
                    }
                });
        },
        onLoad: function (options) {
          let windowHeight = wx.getSystemInfoSync().windowHeight;
          console.log(windowHeight)
          const query = wx.createSelectorQuery();
          query.select('#runsekg').boundingClientRect(rect=>{
            let heights = rect.height;
            console.log(heights);
            this.setData({
              scrollHeight:windowHeight-heights
             })
          }).exec();
        

    //    query.select('#runsekg').boundingClientRect();
    //    this.setData({
    //     scrollHeight:windowHeight-99
    //    })
	  //    query.exec((res) => {
    //  let menutop = res[0].bottom;
	  //   console.log(res); 
    //  this.setData({
    // scrollHeight:windowHeight-menutop
    //   })
    // })
   app.func.onPageLoad(this, options);

        },

        onShow:function (){
            this.categories();
            this.myinfos();
        },

        initData: function (res) {
      
            let value = wx.getStorageSync('category_id');
            if(value){
              console.log(res)
              res.data.forEach((v,i)=>{
                if(v.id ==value){
                  this.setData({
                    tabname:res.data[i].name,
                    currindex:i,
                    scrolltop:"0"
                  })
         
                }
              }) 
              this.getskuarr1(value);

            } else{
              this.getskuarr(res.data[1].id);
            } 
                
            // this.requestnav1(result);
           
            wx.removeStorageSync('category_id');
        },


        preRequestCheckid: function (e) {
            this.getBrandlist(e.target.dataset.id);
            let result = this.packageJson({id: e.target.dataset.id, type: 0});
            this.requestnav1(result);
        },


        /**
         * 页面上拉触底事件的处理函数
         */
        // onReachBottom: function () {
        //     if (this.data.pageEnd) {
        //         this.setData({pageEnd: true});
        //     } else {
        //         wx.showLoading({title: '加载更多', mask: true});
        //         let data = this.data;
        //         let result = this.packageJson({
        //             id: data.id,
        //             offset: data.productlist[data.currentTab].length,
        //             min: data.min,
        //             max: data.max,
        //             min_rate: data.min_rate,
        //             max_rate: data.max_rate,
        //             type: data.type,
        //             key: data.key,
        //             sort: data.sort,
        //             brand: data.brand,
        //         });
        //         this.requestnav1(result);
        //     }
        // },

        //处理需要提交的数据
        packageJson: function (obj) {
            let id = obj.id === 0 || obj.id ? obj.id : this.data.id;
            let offset = obj.offset || 0;
            let type = obj.type || 0;
            let key = obj.key || '';
            let min = obj.min || 0;
            let max = obj.max || 10000;
            let min_rate = obj.min_rate || '';
            let max_rate = obj.max_rate || '';
            let sort = obj.sort || '';
            let sup_id = obj.sup_id || '';
            let brand = obj.brand || '';
            let result = {
                id: id,
                offset: offset,
                type: type,
                key: key,
                min: min,
                max: max,
                min_rate: min_rate,
                max_rate: max_rate,
                sort: sort,
                length: 20,
                sup_id: sup_id,
                brand: brand
            };
            this.setData({
                id: id, type: type, min: min, max: max, sup_id: sup_id,
                key: key, sort: sort, min_rate: min_rate, max_rate: max_rate,
                brand: brand,
            });
            return result;
        },
        requestnav1: function (obj) {
          console.log(obj)
            app.func.postPromise('/categorySku', obj).then(([code, res]) => {
                let prolist = this.data.productlist;
                let index = this.data.currentTab;
                if (obj.offset === 0) {
                    if (res.data.length === 0) {
                        app.func.toastPromise('该分类下暂无商品');
                    } else {
                        prolist[index] = res.data;
                        this.setData({
                            productlist: prolist,
                            pageEnd: false,
                        });
                        wx.setNavigationBarTitle({title: res.name});
                    }
                } else if (res.data.length === 0) {
                    this.setData({pageEnd: true}, () => wx.hideLoading());
                } else {
                    prolist[index] = prolist[index].concat(res.data);
                    this.setData({productlist: prolist}, () => wx.hideLoading());
                }
            });
        },

        //点击 推荐
        // choosetype: function (e) {
        //     this.scrollTop();
        //     let result = this.packageJson({
        //         brand: this.data.brand,
        //         sup_id: this.data.sup_id,
        //         min: this.data.min,
        //         max: this.data.max,
        //         min_rate: this.data.min_rate,
        //         max_rate: this.data.max_rate,
        //         type: 1
        //     });
        //     this.requestnav1(result);
        // },


        //列表排序
        proListOrder: function (e) {
            this.scrollTop();
            let ordetType = e.currentTarget.dataset.type;
            let sort = this.data.sort;
            if (ordetType == 0) {
                sort = sort && sort == 3 ? 4 : 3;
            } else {
                sort = sort && sort == 1 ? 2 : 1;
            }
            let result = this.packageJson({
                brand: this.data.brand,
                sup_id: this.data.sup_id,
                min: this.data.min,
                max: this.data.max,
                min_rate: this.data.min_rate,
                max_rate: this.data.max_rate,
                sort: sort,
                offset: 0,
            });
            this.setData({
                    sort: sort
                },
                this.requestnav1(result)
            );
        },


        //点击筛选
        chooseall: function () {
            this.companyInfoInit();
            this.setData({showbox: true});
        },
        backbtn: function () {
            this.setData({showbox: false});
        },
        formsubmit(e) {
            this.scrollTop();
            let data = e.detail.value;
            let result = this.packageJson({
                type: this.data.chooseindex,
                key: data.key,
                min: data.minprice,
                max: data.maxprice,
                min_rate: data.minrate,
                max_rate: data.maxrate,
                sup_id: data.company == null ? '' : this.data.company[data.company].id,
                brand: this.data.brand
            });
            this.setData({
                showbox: false
            }, this.requestnav1(result));
            
        },
        myinfos(){
            app.func.getPromise('/my/info')
                    .then(([code, res]) => {
                    this.setData({
                            myinfo: res.message
                    });
            })
        },
        changesku(e){
          let index=  e.currentTarget.dataset.index;
          let item =  e.currentTarget.dataset.item;
          console.log(item)
          let undelist=this.data.runlist[index].subitems;
          wx.showLoading({
            title: '加载中',
          })
          app.func.postPromise('/categorySku', {id:item.id,
            offset:0,
            type:0,
            key: '',
            min:0,
            max:10000,
            min_rate: '',
            max_rate:'',
            sort: '',
            length:100000,
            sup_id:'',
            brand: '',
          
          }).then(res=>{
            wx.hideLoading({
              success: (res) => {},
            })
            console.log(res[1])
            console.log(res)
             if(res[1].data.length==0){
              app.func.toastPromise('该分类下暂无商品');
              this.setData({
                skunameList:[]
              })
             }else{
              this.setData({
                skunameList:res[1].data
              })
             }
          })

          this.setData({
            currindex:index,
            tabname:item.name
          })
        },
        productdetail: function (e) {
          var id = e.currentTarget.dataset.id;
          let ids= this.data.runlist[this.data.currindex].id;
          wx.setStorageSync('category_id', ids);
          wx.navigateTo({
            url: '/pages/index/productdetail/productdetail?id=' + id
          })
        },
        getskuarr:function(id){
          wx.showLoading({
            title: '加载中',
          })
          app.func.postPromise('/categorySku', {id:id,
            offset:0,
            type:0,
            key: '',
            min:0,
            max:10000,
            min_rate: '',
            max_rate:'',
            sort: '',
            length:100000,
            sup_id:'',
            brand: '',
          }).then(res=>{
            wx.hideLoading({
              success: (res) => {},
            })
            console.log(res[1])
            console.log(res)
             if(res[1].data.length==0){
              app.func.toastPromise('该分类下暂无商品');
              this.setData({
                skunameList:[],
                tabname:this.data.runlist[1].name,
                currindex:1
              })
             }else{
              this.setData({
                skunameList:res[1].data,
                tabname:this.data.runlist[1].name,
                currindex:1
              })
             }
          })
        },
        getskuarr1:function(id){
          wx.showLoading({
            title: '加载中',
          })
          app.func.postPromise('/categorySku', {id:id,
            offset:0,
            type:0,
            key: '',
            min:0,
            max:10000,
            min_rate: '',
            max_rate:'',
            sort: '',
            length:100000,
            sup_id:'',
            brand: '',
          }).then(res=>{
            wx.hideLoading({
              success: (res) => {},
            })
            console.log(res[1])
            console.log(res)
             if(res[1].data.length==0){
              app.func.toastPromise('该分类下暂无商品');
              this.setData({
                skunameList:[],
              })
             }else{
              this.setData({
                skunameList:res[1].data,
              
              })
             }
          })
        },
        

    }
);





